//
//  PWDController.swift
//  DSP
//
//  Created by tucici on 2024/5/24.
//

import UIKit
enum PWDControllerType{
    case set
    case reset
}
class PWDController: DSAlertController {
    @IBOutlet var bgView: UIView!
    @IBOutlet var titleLbl: UILabel!
    @IBOutlet var bg_input1: UIView!
    @IBOutlet var bg_input2: UIView!
    @IBOutlet var bg_input3: UIView!
    @IBOutlet var input1: UITextField!
    @IBOutlet var name1: UILabel!
    @IBOutlet var input2: UITextField!
    @IBOutlet var name2: UILabel!
    @IBOutlet var input3: UITextField!
    @IBOutlet var name3: UILabel!
    @IBOutlet var OK: UIButton!
    @IBOutlet var Cancel: UIButton!
    @IBOutlet var bg_input1Height: NSLayoutConstraint!
    @IBOutlet var bgRight: NSLayoutConstraint!
    @IBOutlet var bgLeft: NSLayoutConstraint!
    var block : ((String,String,String)->Bool)?
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
    }
    
    override func initUI() {
        offsetH_KeyboardHidden = 50
        bgLeft.constant = k_w(30)
        bgRight.constant = k_w(30)
    }
    
    var type : PWDControllerType = .reset{
        didSet{
            bg_input1Height.constant = type == .set ? 0 : 34
            bg_input1.isHidden = type == .set
            name1.text = type == .reset ? "Old password:" : ""
            name2.text = type == .reset ? "New password:" : "Password:"
        }
    }
    
    @IBAction func actionForBtn(_ sender: UIButton) {
        switch sender{
        case OK:
            if input1.text?.count ?? 0 <= 0 && type == .reset{
                CLHUD("The old password is incorrect",type: .error,time:1.5, callBack: nil)
                return
            }else if input2.text?.count ?? 0 <= 0 && type == .reset{
                CLHUD("The new password is incorrect",type: .error,time:1.5, callBack: nil)
                return
            }else if input2.text != input3.text && type == .reset{
                CLHUD("The password doesn't match twice",type: .error,time:1.5, callBack: nil)
                return
            }else if input2.text?.count ?? 0 <= 0 && type == .set{
                CLHUD("The password is incorrect",type: .error,time:1.5, callBack: nil)
                return
            }else if input3.text != input2.text && type == .set{
                CLHUD("The password doesn't match twice",type: .error,time:1.5, callBack: nil)
                return
            }
            
            let result = block?(input1.text ?? "", input2.text ?? "", input3.text ?? "") ?? false
            if result {
                CLHUD("Successfully!",type: .success) {[weak self] in
                    self?.dismiss(animated: false)
                }
            }
        case Cancel:
            dismiss(animated: false)
        default:
            break
        }
    }
}

//MARK: - UITextFieldDelegate
extension PWDController:UITextFieldDelegate{
    func textFieldDidChangeSelection(_ textField: UITextField) {
        if textField.markedTextRange != nil{return}
        
    }
    
    
    
}
